home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE02 / CLINIC / EXECWAIT.PAS next >
Encoding:
Pascal/Delphi Source File  |  1995-03-20  |  1.2 KB  |  38 lines

  1. unit ExecWait;
  2. interface
  3.  
  4.   function WinExecAndWait(Path: Pchar; Visibility: Word): Word;
  5.   { Use this code to launch a second program from inside a Turbo
  6.     Pascal for Windows program. It accepts the same parameters as
  7.     WinExec, but will loop continuously until the launched program
  8.     has terminated. This code exists because it is sometimes
  9.     necessary to launch a second program without executing any more
  10.     code in your program. If you did not take this approach, then
  11.     both programs would be executing simultaneously via Windows
  12.     multitasking abilities.
  13.   }
  14.  
  15. implementation
  16. uses WinTypes,
  17.      WinProcs,
  18.      Messages;
  19.  
  20.   function WinExecAndWait(Path: Pchar; Visibility: Word): Word;
  21.   var InstanceID : THandle;
  22.       Msg : TMSg;
  23.   begin
  24.     InstanceID := WinExec(Path,Visibility);
  25.     if InstanceID < 32 then { a value less than 32 indicates an Exec error }
  26.       WinExecAndWait := InstanceID
  27.     else
  28.       repeat
  29.         while PeekMessage(Msg,0,0,0,PM_REMOVE) do
  30.         begin
  31.           if Msg.Message = WM_QUIT then halt(Msg.wParam);
  32.           TranslateMessage(Msg);
  33.           DispatchMessage(Msg);
  34.         end;
  35.       until GetModuleUsage(InstanceID) = 0;
  36.   end {WinExecAndWait};
  37. end.
  38.